package com.wc.AlgoOJ._DFS.撞球;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/9/29 22:19
 * @description http://43.138.190.70:8888/p/AG0305
 */
public class Main {
    /**
     * 思路：迭代加深
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 25;
    static int[][] g = new int[N][N];
    static int[] dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1};
    static int n, m;
    static int sx = 0, sy = 0, ex = 0, ey = 0;

    public static void main(String[] args) {
        while ((m = sc.nextInt()) != 0 && (n = sc.nextInt()) != 0) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    g[i][j] = sc.nextInt();
                    if (g[i][j] == 2) {
                        sx = i;
                        sy = j;
                        g[i][j] = 0;
                    }
                    if (g[i][j] == 3) {
                        ex = i;
                        ey = j;
                    }
                }
            }
            int ans = 1;
            while (!dfs(sx, sy, 1, ans) && ans <= 10) ans++;
            out.println(ans == 11 ? -1 : ans);
        }
        out.flush();
    }

    static boolean dfs(int x, int y, int u, int depth) {
        if (u > depth) return false;

        for (int i = 0; i < 4; i++) {
            int a = x + dx[i], b = y + dy[i];
            // 不断向一个方向运动
            while (a >= 0 && a < n && b >= 0 && b <= m && g[a][b] == 0) {
                a += dx[i];
                b += dy[i];
            }
            // 滚出去了, 或者他旁边是砖头,或者就是傍边不是3
            if (a < 0 || a >= n || b < 0 || b >= m || g[x + dx[i]][y + dy[i]] != 0 && g[x + dx[i]][y + dy[i]] != 3) continue;
            // 停在3上了
            if (g[a][b] == 3) return true;
            g[a][b] = 0;
            // 回到他旁边的位置
            if (dfs(a - dx[i], b - dy[i], u + 1, depth)) return true;
            g[a][b] = 1;

        }
        return false;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
